﻿using Prism.Ioc;
using Serilog;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Prismx.Cool
{
    public static class AppApplicationBuilderExtensions
    {
        static string LogFilePath(string LogEvent) => $@"{AppContext.BaseDirectory}Logs\{LogEvent}\log.log";
        static string SerilogOutputTemplate = @"{NewLine}时间：{Timestamp:yyyy-MM-dd HH:mm:ss.fff}{NewLine}{Message}{NewLine}{Exception}" + new string('-', 50);
        /// <summary>
        /// 日志配置
        /// </summary>
        /// <param name="application"></param>
        public static void UseSerilog(this Application application)
        {
            try
            {
                Log.Logger = new LoggerConfiguration()

    .Enrich.FromLogContext()
    .MinimumLevel.Verbose() // 所有Sink的最小记录级别
    .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Debug).WriteTo.File(LogFilePath("Debug"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 20))
    .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Verbose).WriteTo.File(LogFilePath("HttpInfo"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 20))
       .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Information).WriteTo.File(LogFilePath("Info"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 20))
    .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Warning).WriteTo.File(LogFilePath("Warning"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 20))
    .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error).WriteTo.File(LogFilePath("Error"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 20))
    .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Fatal).WriteTo.File(LogFilePath("Fatal"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate, rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 20))
    .CreateLogger();
            }
            catch
            {
                Log.Logger = new LoggerConfiguration().WriteTo.File(LogFilePath("Defalut"), //$"{AppContext.BaseDirectory}00_Logs\log.log"
                             rollingInterval: RollingInterval.Day,
                             outputTemplate: SerilogOutputTemplate
                          ).CreateLogger();
            }
        }
        /// <summary>
        /// 样式配置
        /// </summary>
        /// <param name="application"></param>
        public static void UseHandyControl(this Application application)
        {
            try
            {
                var skins = Application.Current.Resources.MergedDictionaries;
                skins.Insert(0, new ResourceDictionary() { Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml") });
                skins.Insert(0, new ResourceDictionary() { Source = new Uri("pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml") });
            }
            catch (Exception ee)
            {
                Log.Error("样式加载失败:" + ee.ToString());
            }
        }
        
    }
}
